import { EEaseType, IVector2, Move, Vector2, _logic, ccclass, winSize } from "../../../main/script/Main";
import { EEntityType } from "../../../main/script/module/define/LogicDefine";
import { Item } from "../Item";
import { Entity } from "./Entity";

const v2T = new Vector2()

@ccclass("CubeAnimEntity")
export class CubeAnimEntity extends Entity {

    public entityType = EEntityType.Effect
    public isPut = true
    public isUpdateStateMackine = true
    public isDestroy = false
    public isMoving() { return false }

    private _move = new Move()

    private get item() { return this.getCacheComponent(Item) }

    protected onStateEnterLoad(): void {
        super.onStateEnterLoad()
        this._move.init(this.node, false, 2000)
    }


    protected onStateUpdateRun(): void {
        super.onStateUpdateRun()
        this._move.onUpdate()
    }

    public startAnim(_configId: number, _startPos: IVector2, _isLeft: boolean, _complete?: () => void) {
        this.setScaleNum(_logic.scaleRatio, false)
        this.item.setConfigFrame(_configId)
        this.item.setIconState(true)
        this.setPositionXY(_startPos, false)

        let animationDuration = 0.3
        this._move.setRunData(4, (runData, _index) => {
            if (_index == 0) {
                runData.target.x = winSize().width * .4
                if (_isLeft) runData.target.x *= -1
                runData.target.y = 0

                let _dis = Vector2.distance(_startPos, runData.target)
                runData.speed = _dis / animationDuration
                runData.ease = EEaseType.quartIn
            }
            else {
                runData.target.x = _logic.cubeSize.width / 2
                if (_isLeft)
                    runData.target.x *= -1
                if (_index == 1 || _index == 2) {
                    if (_index == 2) runData.delay = .3
                    runData.target.y = 0
                    runData.speed = 800
                    runData.ease = EEaseType.elasticOut
                } else {
                    runData.target.y = 400
                    runData.speed = 1500
                    runData.ease = EEaseType.quartIn
                }
            }
        })

        this._move.run(() => {
            this.scene.entityMgr.remove(this)
            if (_complete)
                _complete()
        })
    }

}